﻿using System;
using System.Net;

namespace AIC_Notification
{
    public class BoardIssueReport: FormatedReport
    {
        public string TeacherInContext;

        public BoardIssueReport()
        {
            ReportTitle = String.Format ("Trello Board Issues ({0})", Statistics.ApplicationType == ApplicationType.sof ? "SOF" : "Guidance");
        }

        public override string GetSubject()
        {
            return String.Format("{0} - {1:d}", ReportTitle, Global.Now());
        }

        public override string InnerTableHeader()
        {
            return "<tr><th style=\"width: 20%; font-size: 12pt;\">Student</th><th style=\" width: 15%; font-size: 12pt;\">List</th><th style=\" width: 15%; font-size: 12pt;\">Card</th><th style=\" width: 50%; font-size: 12pt;\">Issues</th></tr>";
        }

        public override string EmptyInnerTable()
        {
            return "<tr><td style=\"padding: 10px 0 10px 0; text-align:center;\">No Issue Found</td></tr>";
        }

        public override string GenerateInnerTable()
        {
            string tableBody = "";

            if ((Global.InputErrors != null) && (Global.InputErrors.Count > 0))
            {
                foreach (var IE in Global.InputErrors)
                {
                    if (IE.ErrorOwner == Error.Owner.ac)
                    {
                        if (IE.Application != null)
                        {
                            if (TeacherInContext == IE.Application.AC_PT)
                            {
                                tableBody += String.Format("<tr>{0}<td style=\"font-size: 11pt;\">{1}</td><td style=\"font-size: 11pt;\"><a href=\"{2}\">{3}</a></td><td style=\"font-size: 11pt;\">{4}</td></tr>",
                                                           InsertStudentName(IE.Application, 12),
                                                           WebUtility.HtmlEncode(IE.ListName),
                                                           WebUtility.HtmlEncode(IE.CardUrl),
                                                           WebUtility.HtmlEncode(IE.CardName),
                                                           WebUtility.HtmlEncode(IE.ErrorMessage));
                            }
                        }
                        else
                        {
                            Global.SysUtil.Log(String.Format("Board Issues Null Error: {0}", IE.ErrorMessage), 2);
                        }
                    }
                    else if (IE.ErrorOwner == Error.Owner.admin)
                    {
                        if (TeacherInContext == "")
                        {
                            if (IE.Application != null)
                            {
                                tableBody += String.Format("<tr>{0}<td style=\"font-size: 11pt;\">{1}</td><td style=\"font-size: 11pt;\"><a href=\"{2}\">{3}</a></td><td style=\"font-size: 11pt;\">{4}</td></tr>",
                                                           InsertStudentName(IE.Application, 12),
                                                           WebUtility.HtmlEncode(IE.ListName),
                                                           WebUtility.HtmlEncode(IE.CardUrl),
                                                           WebUtility.HtmlEncode(IE.CardName),
                                                           WebUtility.HtmlEncode(IE.ErrorMessage));
                            }
                            else
                            {
                                tableBody += String.Format("<tr><td style=\"font-size: 11pt;\"></td><td style=\"font-size: 11pt;\"></td><td style=\"font-size: 11pt;\"></td><td style=\"font-size: 11pt;\">{0}</td></tr>",
                                                           WebUtility.HtmlEncode(IE.ErrorMessage));
                            }
                        }
                    }
                }
            }

            return tableBody;
        }

        public override void SendReportAsEmail()
        {
            string HTMLBody;

            // Generate Report for ACs
            foreach (var _teacher in TeacherDB.Teachers)
            {
                if ((_teacher.Type == "Academic Consultant" || _teacher.Type == "Both") && _teacher.Students.Count > 0)
                {
                    TeacherInContext = _teacher.Name;
                    HeaderInformation = _teacher.Name;

                    FileName = String.Format("{0}-{1}-{2:yyyyMMdd-HHmmss}.htm", ReportTitle, _teacher.Name, Global.Now());
                    Global.SysUtil.Log("Generating Report: " + FileName);

                    HTMLBody = GetHTMLBody();

                    if (!IsEmptyBody)
                    {
                        // Save Report to File
                        SaveReportToFile(FileName, HTMLBody);

                        // Send Report E-mail
                        toRecipients.Clear();
                        ccRecipients.Clear();

                        toRecipients.Add(_teacher.Email);

                        string[] SystemAdmins = TeacherDB.GetSystemAdmin();
                        foreach (var SystemAdmin in SystemAdmins)
                            ccRecipients.Add(SystemAdmin);

                        SubjectID = _teacher.Name;

                        Global.SysUtil.SendHTML(toRecipients, ccRecipients, GetSubject(), HTMLBody);
                    }
                }
            }

            // Generate Report for Admin
            TeacherInContext = "";
            HeaderInformation = "Admin";

            FileName = String.Format("{0}-{1}-{2:yyyyMMdd-HHmmss}.htm", ReportTitle, "Admin", Global.Now());
            Global.SysUtil.Log("Generating Report: " + FileName);

            HTMLBody = GetHTMLBody();

            if (!IsEmptyBody)
            {
                // Save Report to File
                SaveReportToFile(FileName, HTMLBody);

                // Send Report E-mail
                toRecipients.Clear();
                ccRecipients.Clear();

                string[] SystemAdmins = TeacherDB.GetSystemAdmin();
                foreach (var SystemAdmin in SystemAdmins)
                    ccRecipients.Add(SystemAdmin);

                SubjectID = "";

                Global.SysUtil.SendHTML(toRecipients, ccRecipients, GetSubject(), HTMLBody);
            }
        }
    }
}
